local CorePropExchangePointView = class("ExchangePointView", app.mvc.ViewBase)
local Armature = require("app.public.avatar.Armature")
CorePropExchangePointView.VIEW_ROOT_RES = Res.CorePropExchangePointViewCsb
local CorePropUIHelp = require("app.coreProp.CorePropUIHelp")
local ProgressSpeed = 10
local Item = require("app.public.dao.Item")

function CorePropExchangePointView:init()
    display.setCascadeOpacityEnabled(self._root, true)
    self._ui.bg:setTouchEnabled(true)
    display.uiAddClick(self._ui.bg, function()
        self:closeView()
    end)
    self._ui.Button_close:addClickEventListener(function()
        self:closeView()
    end)
	self._uiLeftPoint = self._ui["Panel/Panel_content/num_point"]
	
	

	self._ui["Panel/Panel_content/Panel_bar/tlt"]:setString(L("CoreProp_Needtips"))
	self._uiBar = self._ui["Panel/Panel_content/Panel_bar/LoadingBar"]
	self._uiBarStr = self._ui["Panel/Panel_content/Panel_bar/tlt_0"]
	self._uiAniNode = self._ui["Panel/Panel_content/Node_1"]
	self._uiAniPanel = self._ui["Panel/Panel_content/Panel_ani"]
	
	self._timeline:setLastFrameCallFunc(handler(self, self._onAnimationPlayEnd))

    local itemTemp = Item.new( Enums.ItemBagType.item )
	itemTemp:read( { tid = Constant.CoreProp_ItemId } )
	self._itemData = itemTemp
    self._ui["Panel/Panel_content/icon/icon"]:loadTexture(itemTemp:getIcon())
	self._ui["Panel/Panel_content/icon/bg4"]:loadTexture(itemTemp:getIconBg())
	self._ui["Panel/Panel_content/icon/bg4"]:setTouchEnabled(true)
	display.uiAddClick(self._ui["Panel/Panel_content/icon/bg4"], function()
		self:showTip()
	end)
    
    self:addExchangePointEvent()
    self._ui["Panel/Button_2/name"]:setString(L("CorePropExchangePointBtn2"))
    self._ui["Panel/Button_1/name"]:setString(L("CorePropExchangePointBtn1"))
    self._ui["Panel/Button_1"]:addClickEventListener(handler(self, self._onClickBtn1))

    self._addPonitNum = 0
	self._isPlayAnimation = false
	self._maxSumPoint = CorePropUIHelp.getMaxExchangePoint()
	self._tempItemNum = 0
	self._tempItemCost = 0
    self._tempSumPoint = 0
    self:onUpdate(handler(self, self._onUpdate))
end

function CorePropExchangePointView:setViewData(viewData)
	self._viewData = viewData
	self:refresh()
end


function CorePropExchangePointView:_onClickBtn1()
	self:closeView()
	local params = {
		id = Constant.FUNC_TYPE_SHOP,
		mode = "open",
		tabKey = Constant.FUNC_TYPE_SHOP,
		shopIndex = 2,
		itemType = 1,
		itemId = Constant.CoreProp_ItemId
	}
	UIHandler:handleMessage(params)
end

function CorePropExchangePointView:refresh()
    self:refreshBar()
end

function CorePropExchangePointView:refreshBar()
	if not self._viewData then
		return
	end
	if self._isPlayAnimation then
		return
	end
	local itemNum = CorePropUIHelp.getCorePropItemNum()
	local itemCost = CorePropUIHelp.getCorePropItemCost(self._viewData.sumPoint)
	self._tempItemNum = itemNum
	self._tempItemCost = itemCost
	self._tempSumPoint = self._viewData.sumPoint

	local progress = itemNum * 100 / itemCost
	if progress >= 100 then
		progress = 100
		self._ui["Panel/Button_2"]:setBright(true)
	else
		self._ui["Panel/Button_2"]:setBright(false)
	end

	self._uiBar:setPercent(progress)

	self._uiBarStr:setString(string.format("%d/%d", itemNum, itemCost))
	self._uiLeftPoint:setString(string.format("%s%d", L("CoreProp_LeftPoint"), self._viewData.leftPoint))

	if self._viewData.sumPoint >= self._maxSumPoint then
		self._ui["Panel/Button_1"]:setVisible(false)
		self._ui["Panel/Button_2"]:setVisible(false)
		self._ui["Panel/text_max"]:setVisible(true)
	else
		self._ui["Panel/text_max"]:setVisible(false)
		self._ui["Panel/Button_1"]:setVisible(true)
		self._ui["Panel/Button_2"]:setVisible(true)
	end
end


function CorePropExchangePointView:_onUpdate()
	if self._addPointBtnInTouch and not self._isLongPressTrigger then
		-- 长按
		local t = app:getTime()
		if t - self._startLongPressTime > 1 then
			self._isLongPressTrigger = true
			self:_startExchangeOne()
		end
	end

end

function CorePropExchangePointView:_touchStart()
	-- if self._isPlayAnimation then
	-- 	return
	-- end
	if not self._viewData then
		return
	end
	if self._viewData.sumPoint >= self._maxSumPoint then
		return
	end
	self._isInPlaying = self._isPlayAnimation
	self._addPointBtnInTouch = true
	self._startLongPressTime = app:getTime()
	self._isLongPressTrigger = false
	self._addPonitNum = 0
end

function CorePropExchangePointView:_touchEnd()
	if self._isLongPressTrigger and self._addPonitNum > 0 then
		executeFunc(self._onOperate, "exchangePoint", self._addPonitNum)
	elseif not self._isLongPressTrigger then
		if self:_startExchangeOne() then
			executeFunc(self._onOperate, "exchangePoint", 1)
		end
	end
	self._addPonitNum = 0 
	self._addPointBtnInTouch = false
	self._isInPlaying = false
	self._isLongPressTrigger = false

	if self._tempItemNum < self._tempItemCost and self._tempItemCost ~= 0 then
		self._ui["Panel/Button_2"]:setBright(false)
	end
end

function CorePropExchangePointView:_startExchangeOne()
	
	if self._isInPlaying then
		return
	end

	if self._tempSumPoint >= self._maxSumPoint then
		return false
	end

	if self._tempItemNum < self._tempItemCost then
		display.pushToast(L("CorePropItemNotEnough"))
		return false
	end
	self._tempSumPoint = self._tempSumPoint + 1
	self._tempLastItemCost = self._tempItemCost
	self._tempItemNum = self._tempItemNum - self._tempLastItemCost
	self._tempItemCost = CorePropUIHelp.getCorePropItemCost(self._tempSumPoint)
	self._addPonitNum = self._addPonitNum + 1

	self._uiLeftPoint:setString(string.format("%s%d", L("CoreProp_LeftPoint"), self._viewData.leftPoint + self._addPonitNum))
	local progress = self._tempItemNum * 100 / self._tempItemCost
	if progress > 100 then
		progress = 100
	end
	self._uiBar:setPercent(progress)
	self._uiBarStr:setString(string.format("%d/%d", self._tempItemNum, self._tempItemCost))	
	self:playExchangeAnimation()
	return true
end

function CorePropExchangePointView:addExchangePointEvent()
	local exchangeBtn = self._ui["Panel/Button_2"]
	exchangeBtn:addTouchEventListener(function(sender, eventType)
		if eventType == ccui.TouchEventType.began then
			self:_touchStart()
		elseif eventType == ccui.TouchEventType.ended or eventType == ccui.TouchEventType.canceled then
			self:_touchEnd()
		end
    end)
end


function CorePropExchangePointView:showTip()
	if self._isShowTips then
		return
	end
	local param = { itemId=tonumber(self._itemData.tid) }
	param.dataTemp = self._itemData
	param.tipsType = Enums.TipsType.item
	param.unShowOrigin = self._unShowOrigin
	param.closeCallBack = function(tp)
		if not tolua.isnull( self ) then
			self._isShowTips = false
			if tp == Enums.TIPS_HIDE.GOTO then
				self:closeView()
			end
		end
	end
	self._isShowTips = true
	local parentNode = self._ui["Panel/Panel_content/icon/bg4"]
	local boundingBoxTemp = parentNode:getBoundingBox()
	local worldPosTemp = parentNode:getParent():convertToWorldSpace( cc.p( boundingBoxTemp.x, boundingBoxTemp.y ) )
	boundingBoxTemp.x = worldPosTemp.x
	boundingBoxTemp.y = worldPosTemp.y + 100
	param.boundingBox = boundingBoxTemp
	app:dispatchCustomEvent("EVENT_OPEN_FLOAT", {params=param})
end

function CorePropExchangePointView:playExchangeSuccessAmi()
	if not self._successArmature then
		local params = {
			armatureName = "QH1",
			path = Res.EquipStrengthSuccessAmi
		}
		local armature = Armature.create(params)
		armature:setPosition(0, 0)
		self._uiAniNode:addChild(armature)
		self._successArmature = armature
	end
	self._successArmature:play("QH1", 1)
end

function CorePropExchangePointView:playExchangeAnimation()
	self._isPlayAnimation = true
	self:playAction("animation", false)
	self._uiAniPanel:setVisible(true)
end

function CorePropExchangePointView:_onAnimationPlayEnd()
	if self._isPlayAnimation then
		self:playExchangeSuccessAmi()
		if not self._isLongPressTrigger then
			self._isPlayAnimation = false
			self._uiAniPanel:setVisible(false)
		else
			if not self:_startExchangeOne() then
				self._isPlayAnimation = false
				self._uiAniPanel:setVisible(false)
			end
		end
	end

end

function CorePropExchangePointView:checkGuide()
	local params = {
		scene = Constant.FUNC_TYPE_CORE_PROP,
		sceneSelf = self,
		getFunc = self.getGuideNodeFunc,
		nextFunc = self.checkGuide
	}
	EM:notify("checkGuide", params)
end

-- 获取引导node
function CorePropExchangePointView:getGuideNodeFunc(id)

end

return CorePropExchangePointView